# Create your views here.
from django.db.models import Q
from django.forms import model_to_dict
from rest_framework.views import APIView
from components.models import SysMenu, PackageManagement
from general.Transform import list_to_tree
from utils.views import Result

class PackageManage(APIView):
    def get(self, request):
        data = request.query_params
        sys_menu = PackageManagement.objects.all().values()
        sys_menu_data = list(sys_menu)
        return Result.success_response(data=sys_menu_data, msg='执行成功')

    def post(self, request):
        try:
            data = request.data
            data['status'] = 1
            PackageManagement.objects.create(**data)
            respData = Result.success_response(data=[], msg='执行成功')
        except Exception as e:
            respData = Result.fail_response(data=[], msg='执行失败')
        return respData

    def put(self, request):
        try:
            data = request.data
            rowId = data.get("id")
            PackageManagement.objects.filter(id=rowId).update(**data)
            respData = Result.success_response(data=[], msg='修改成功')
        except Exception as e:
            respData = Result.fail_response(data=[], msg='执行失败')
        return respData

    def delete(self, request):
        try:
            data = request.data
            dataId = data.get("id", "")
            PackageManagement.objects.filter(id=dataId).update(status=0)
            Result.success_response(data=[], msg='执行成功')
        except Exception as e:
            Result.fail_response(data=[], msg='执行失败')

